<template>
  <!-- 没有子路由的情况 -->
  <template v-for="item in props.menuData">
    <el-menu-item
      @click="handleClick(item, `${props.index}-${item.meta.id}`)"
      v-if="!item.children || item.children.length == 0"
      :key="`${props.index}-${item.meta.id}`"
      :index="`${props.index}-${item.meta.id}`"
    >
      <el-icon size="20">
        <component :is="item.meta.icon" />
      </el-icon>
      <span>{{ item.meta.name }}</span>
    </el-menu-item>
    <!-- 有子路由的情况 -->
    <el-sub-menu v-else :index="`${props.index}-${item.meta.id}`">
      <template #title>
        <el-icon size="20">
          <component :is="item.meta.icon" />
        </el-icon>
        <span>{{ item.meta.name }}</span>
      </template>
      <tree-menu :index="`${props.index}-${item.meta.id}`" :menuData="item.children" />
    </el-sub-menu>
  </template>
</template>

<script setup>
  import { useRouter } from 'vue-router'
  import { useStore } from 'vuex'
  const store = useStore()
  const router = useRouter()
  const props = defineProps(['menuData', 'index'])

  console.log(props.menuData, 'props.menuData')
  // 点击菜单
  const handleClick = (item, active) => {
    store.commit('addMenu', item.meta)
    router.push(item.meta.path)
  }
</script>

<style lang="scss" scoped></style>
